#include "processbar.h"

//设计进度条
char* lable = "/-\\-";
char bar[NUM];


void initbar()
{
  memset(bar,'\0',sizeof(bar));
}

void processbar(int curr)
{
  if(curr<0 || curr>100)
    return;

  int len = strlen(lable);


  //printf("[%-100s][%d%%][%c]\r",bar,curr,lable[curr%len]);
  //添加颜色
  
  printf(RED"[%-100s]"NONE"[%d%%][%c]\r",bar,curr,lable[curr%len]);
  fflush(stdout);
  bar[curr++] = PRO;
  if(curr<100)
    bar[curr] = PON;
}

void download(processBar cb)
{
  int total = 1000;
  int star = 0;

  while(star<=total)
  {
    usleep(50000);//模拟下载时间
    int curr = star*100/total;
    cb(curr);
    star += 100;
  }
  printf("\n");
}


/*
const char *lable="|/-\\";
char bar[NUM];

#define GREEN "\033[0;32;32m"
#define NONE "\033[m"

// v2: 是如何被调用的
void processbar(int rate)
{
    if(rate < 0 || rate > 100) return;

    int len = strlen(lable);
    printf(GREEN"[%-100s]"NONE"[%d%%][%c]\r", bar, rate, lable[rate%len]); // 没有\n,就没有立即刷新，因为显示器模式是行刷新
    fflush(stdout);
    bar[rate++] = BODY;
    if(rate < 100) bar[rate] = RIGHT;
}

void initbar()
{
    memset(bar, '\0', sizeof(bar));
}*/
